package ift585.tp3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Utility {
	
	private static final String ORDI_MARC = "Windows 7";
	private static final String LINUX = "Linux";
	
	static public void popHtmlFile(String fileName) throws IOException {
		String pathToFirefox = "";
		String path = System.getProperty("user.dir") + System.getProperty("file.separator") + fileName;
		if(System.getProperty("os.name").equals(LINUX)){
			pathToFirefox = "firefox";
			Runtime.getRuntime().exec(pathToFirefox + " " + path);
		}
		else if(System.getProperty("os.name").equals(ORDI_MARC)) {
			pathToFirefox = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";
			Runtime.getRuntime().exec(pathToFirefox + " " + path);
		}
	}
	
	static public void inputStreamToFile(InputStream input, String fileName) throws IOException{

		FileOutputStream out = new FileOutputStream(new File(fileName));

		int oneByte;
		
		while((oneByte = input.read()) != -1){
			out.write(oneByte);
		}
		out.flush();
		out.close();

	}
	
	static public String inputStreamToStringAndFile(InputStream input, String fileName) throws IOException{
		String result = "";
		
		FileOutputStream out = new FileOutputStream(new File(fileName));
		int oneByte;
		
		while((oneByte = input.read()) != -1){
			out.write(oneByte);
			result += (char)oneByte;
		}

		out.close();
		input.close();
		return result;
	}
}
